package com.springcloud.market.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.springcloud.market.order.dto.AddOrder;
import com.springcloud.market.order.entity.Order;
import com.springcloud.market.order.service.IOrderService;
import com.springcloud.market.user.service.UserService;
import com.springcloud.market.order.feign.StockFeignService;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@RefreshScope
public class OrderController {
    //    @Value("${user.name}")
    //    private String name;
    //    @Value("${user.age}")
    //    private Integer age;
    //
    //
    //
    //    @RequestMapping("/info")
    //    public String info(){
    //        return "姓名："+name+"，年龄："+age;
    //    }

    @Autowired
    private StockFeignService stockFeignService;
    @DubboReference
    private UserService userService;
    //2025-09-30
    @Resource
    private IOrderService orderService;

    @RequestMapping("/add")
    public String add(){
        return stockFeignService.reduce();
    }

    @RequestMapping("/getUserInfo")
    public String getUserInfo(){
        return userService.getUserInfo();
    }

    //第三次作业
    @RequestMapping("/getUserInfo1")
    public String work(){
        return stockFeignService.work();
    }

    @RequestMapping("/readOrder")
    public String rederOrder(){
        System.out.println("网关测试：order服务");
        return "网关测试：order服务";
    }

    //2025-09-30课
    @PostMapping("/addOrder")
    public String addOrder(@RequestBody AddOrder order) {
        Integer num = order.getNum();
        Integer productId = order.getProductId();
//        return stockFeignService.reduceStock(productId, num);
        return orderService.createOrder(order.getProductId(), order.getNum());
    }

    //2025-10-21课
    @GetMapping("/createOrder")
    @SentinelResource(value = "orderApi", blockHandler = "handleFlowBlock")
    public String createOrder() {
        return "订单创建成功! (Time: " + System.currentTimeMillis() + ")";
    }
    public String handleFlowBlock(BlockException ex) {
        return "请求过于频繁，请稍后再试! (触发限流)";
    }
    @GetMapping("/getUser")
    @SentinelResource(value = "userApi", blockHandler = "handleDegradeBlock")
    public String getUser(){
        // 模拟业务逻辑：随机产生异常，模拟服务不稳定
        if (Math.random() > 0.7) {
            throw new RuntimeException("查询用户信息失败，数据库异常");
        }
        return userService.getUserInfo();
    }
    public String handleDegradeBlock(BlockException ex) {
        return "服务暂时不可用，请稍后重试! (触发熔断)";
    }

    //2025-10-28课
    @GetMapping("/createOrder2")
    public Order createOrder2(){
        return orderService.createOrder();
    }



}